<!--
/**
* Author: linchun
* Date: 2022-07-27 11:23
* Desc: 扫码 QRcode 
*/
-->
<template>
  <view class="flex-s-m">
    <image
      @click="handelClick"
      class="img-icon"
      mode="scaleToFill"
      :src="'https://public-oss-file.zmaxfilm.com/applet/img/film-oxygen-uniapp/icon-scan.png'"
    ></image>
  </view>
</template>

<script>
  //#ifdef H5
  import { scanQRCode } from '@/utils/sdk'
  //#endif
  import { param2Obj } from '@/utils/index'
  import { navigateTo } from '@/utils/tools'
  export default {
    name: 'q-rcode',
    data() {
      return {
        // visible: true
      }
    },
    created() {
      // if (window.location.href.includes('CQR=1')) {
      //   this.visible = false
      // }
    },
    methods: {
      // 初始化方法
      handelClick() {
        //#ifdef MP-ALIPAY
        uni.scanCode({
          success(res) {
            this.handleScanResult(res, this)
          },
          fail(err) {
            console.log(err)
            this.$msg.toast('请扫描正确二维码')
          }
        })
        //#endif
        //#ifdef MP-WEIXIN || MP-TOUTIAO
        this.miniProgramScanCode(this)
        //#endif
        //#ifdef H5
        this.H5ScanCode(this)
        //#endif
      },
      miniProgramScanCode(vm) {
        uni.authorize({
          scope: 'scope.camera',
          success: () => {
            uni.scanCode({
              success(res) {
                vm.handleScanResult(res, vm)
              },
              fail(err) {
                console.log(err)
                vm.$msg.toast('请扫描正确二维码')
              }
            })
          },
          fail: () => {
            //授权失败
            uni.showModal({
              title: '该功能需摄像头授权',
              content: '是否进入权限管理，调整授权？',
              success: (res) => {
                if (res.confirm) {
                  //调起客户端小程序设置界面，返回用户设置的操作结果。（重新让用户授权）
                  uni.openSetting({
                    success: (res) => {
                      console.log(res.authSetting)
                    }
                  })
                } else if (res.cancel) {
                  return uni.showToast({
                    title: '已取消！'
                  })
                }
              }
            })
          }
        })
      },
      H5ScanCode(vm) {
        scanQRCode()
          .then((res) => {
            vm.handleScanResult(res, vm)
          })
          .catch((err) => {
            console.log(err)
            // vm.$msg.toast('请扫描正确二维码_H5')
          })
      },
      handleScanResult(res, vm) {
        let result = ''
        //#ifdef MP-WEIXIN || MP-ALIPAY || MP-TOUTIAO
        result = res.result || ''
        //#endif
        //#ifdef H5
        result = res || ''
        //#endif
        if (result && result.indexOf('https') !== -1) {
          //是链接标识为商户码
          const resultObj = param2Obj(result)
          const { businessId } = resultObj
          navigateTo('/pagesOrder/merchantsCoupon/index', {
            businessId
          })
        } else {
          //扫券码
          vm.handleCouponScan(result, vm)
        }
      },
      handleCouponScan(result, vm) {
        uni.$api.card.qrCodeBind({ qrCode: result || '' }).then((resp) => {
          console.log('resp222', resp)
          if (resp.message) {
            uni.showModal({
              title: '绑定成功',
              content: resp.message,
              confirmText: '查看',
              success: (res) => {}
            })
          } else {
            vm.$msg.toast('绑定成功')
          }
        })
        // .catch((err) => {
        //   console.log(212424544634545, err)
        //   vm.$msg.toast((err.data && err.data.content) || '绑定失败!')
        // })
      }
    }
  }
</script>
<style lang="scss" scoped>
  .img-icon {
    width: 44rpx;
    height: 40rpx;
  }
</style>
